package org.hzdl.order.service.impl;


import org.hzdl.order.entity.Goods;
import org.hzdl.order.entity.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OrderServiceImpl implements OrderService {
    //初始化订单集合
    private static final Map<Integer, Order> orderMap = new HashMap<>();
    static {
        Order order1 = new Order(1, 1, "2020-11-11 00:01");
        Order order2 = new Order(2, 2, "2020-11-11 00:02");
        Order order3 = new Order(3, 3, "2020-11-11 00:03");
        orderMap.put(order1.getId(), order1);
        orderMap.put(order2.getId(), order2);
        orderMap.put(order3.getId(), order3);
    }
    //自动注入RedisTemplate对象
    @Autowired
    private RestTemplate restTemplate;
    //调用商品微服务，根据商品ID查询商品详情
    private Goods findGoodsById(Integer id) {
        //使用RestTemplate访问商品微服务的查询接口
        ResponseEntity<Goods> responseEntity = restTemplate.getForEntity("http://goods/goods/one?id="+id, Goods.class);
        return responseEntity.getBody();
    }
    @Override
    public List findAll(){
        //定义要返回的订单集合
        List orderList = new ArrayList();
        //遍历初始化的订单集合
        orderMap.forEach((k,v)->{
            //调用findGoodsById方法根据商品ID查询商品对象
            Goods goods = findGoodsById(v.getGoodsId());
            //定义包含商品信息的订单信息
            HashMap order = new HashMap();
            order.put("订单ID",k);
            order.put("下单时间",v.getTime());
            order.put("商品ID",v.getGoodsId());
            order.put("商品名称",goods.getName());
            order.put("商品价格",goods.getPrice());
            //将订单信息添加到订单集合中
            orderList.add(order);
        });
        //返回订单集合
        return orderList;
    }
    @Override
    public Object findById(Integer id){
        //获取相应ID的订单对象
        Order o = orderMap.get(id);
        //调用findGoodsById方法根据商品ID查询商品对象
        Goods goods = findGoodsById(o.getGoodsId());
        //定义包含商品信息的订单信息
        HashMap order = new HashMap();
        order.put("订单ID",id);
        order.put("下单时间",o.getTime());
        order.put("商品ID",o.getGoodsId());
        order.put("商品名称",goods.getName());
        order.put("商品价格",goods.getPrice());
        //返回订单信息
        return order;
    }
}
